package com.group01.community.controller;


import com.group01.community.entity.vo.SysUserVo;
import com.group01.community.service.SysUserService;
import com.group01.community.utils.JwtUtil;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.ResponseStatus;
import com.group01.community.utils.RsaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.group01.community.entity.SysRole;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author : iuoly
 * date : 2022/7/6
 */
@RestController
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/info")
    public ResponseResult info(String token) {
        String username = JwtUtil.getJwtKeyUsername(token, RsaUtil.publicKey);
        SysUserVo userRole = sysUserService.getUserRoleByUsername(username);
        List<SysRole> role = userRole.getUserRole();
        ArrayList<Object> roles = new ArrayList<>();
        role.forEach(a -> roles.add(a.getRoleName()));
        HashMap<String, Object> map = new HashMap<>();
        map.put("roles", roles);
        map.put("introduction", userRole.getRemark());
        map.put("avatar", userRole.getAvatar());
        map.put("name", username);
        map.put("info", userRole);
        return new ResponseResult(ResponseStatus.OK,  map);
    }

    @GetMapping("/checkInfo")
    public ResponseResult checkInfo(String name) {
        SysUserVo userRole = sysUserService.getUserRoleByUsername(name);
        return new ResponseResult(ResponseStatus.OK,  userRole);
    }
}
